<?php

namespace App\Http\Controllers;

use App\Models\Site;
use App\Models\Article;

class TopController extends Controller
{

    public function showPages()
    {
        $flag = true;
        if ($flag) {
        }
    }

    public function index()
    {
        $site           = new Site();
        $site['all']    = $site->orderBy('views', 'desc')->where('state',1)->take(10)->get();
        $site['day']    = $site->whereDate('updated_at',$this->day())->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        $site['week']   = $site->whereBetween('updated_at', [$this->week()[0],$this->week()[1]])->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        $site['month']  = $site->whereBetween('updated_at', [$this->month()[0],$this->month()[1]])->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        $article            = new Article();
        $article['all']     = $article->orderBy('views', 'desc')->where('state',1)->take(10)->get();
        $article['day']     = $article->whereDate('updated_at',$this->day())->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        $article['week']    = $article->whereBetween('updated_at', [$this->week()[0],$this->week()[1]])->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        $article['month']   = $article->whereBetween('updated_at', [$this->month()[0],$this->month()[1]])->where('state',1)->orderBy('views', 'desc')->take(10)->get();
        return view('top.index',compact('site','article'));
    }
    /**
     * 返回本周开始和结束的时间戳
     *
     * @return array
     */
    public static function day()
    {
        return date("Y-m-d",time());
    }
    /**
     * 返回本周开始和结束的时间戳
     *
     * @return array
     */
    public static function week()
    {
        $timestamp = time();
        return [
            date('Y-m-d', strtotime("this week Monday", $timestamp)),
            date('Y-m-d', strtotime("this week Sunday", $timestamp)+ 24 * 3600 - 1),
        ];
    }
    /**
     * 返回本月开始和结束的时间戳
     *
     * @return array
     */
    public static function month()
    {
        return [
            date('Y-m-d',mktime(0, 0, 0, date('m'), 1, date('Y'))),
            date('Y-m-d',mktime(23, 59, 59, date('m'), date('t'), date('Y')))
        ];
    }
}
